﻿# provides the constant class generator

import constant
import galaxy_reader
import os
import sys

# the namespace for all generated constants
CONSTANTS_NAMESPACE = "Vestras.StarCraft2.Grape.Galaxy.Interop.NativeApi.Constants"

# the directory path for all generated constants
CONSTANTS_DIRECTORY_PATH = "constants\\"

new_line = "\n"
indent_level = 0

# utility functions
def tab():
    result = ""
    for c in range(0, indent_level):
        result += "\t"
    
    return result

def write_start_brace():
    global indent_level
    s =  "{" + new_line
    indent_level = indent_level + 1
    return s
    
def write_end_brace():
    global indent_level
    indent_level = indent_level - 1
    s = tab() + "}" + new_line
    return s

def write_header():
    return tab() + "// This code was generated by a tool." + new_line + new_line

def write_usings():
    s = ""
    s += tab() + "using System;" + new_line
    s += tab() + "using System.CodeDom.Compiler;" + new_line + new_line
    return s

def write_namespace():
    s = ""
    s += tab() + "namespace " + CONSTANTS_NAMESPACE + " "
    s += write_start_brace();
    return s
    
def write_attributes(type, name):
    s = ""
    s += tab() + "[GeneratedCode(\"generate_constants.py\", \"1.0.0.0\")]" + new_line
    s += tab() + "[GalaxyConstant(\"" + type + "\", \"" + name + "\")]" + new_line
    return s
    
def write_classdecl(name):
    s = ""
    s += tab() + "public sealed class " + name + " "
    s += write_start_brace()
    return s

def write_end_classdecl():
    return write_end_brace()
    
def write_end_namespace():
    return write_end_brace()

for c in galaxy_reader.constant_elements:
	print 'Generating constant class for: ' + c.get_name()

	# write C# code to file
	indent_level = 0
	s = ""
	s += write_header()
	s += write_usings()
	s += write_namespace()
	s += write_attributes(c.get_type(), c.get_name())
	s += write_classdecl(c.get_name())
	s += write_end_classdecl()
	s += write_end_namespace()
	f = open(os.getcwd() + "\\" + CONSTANTS_DIRECTORY_PATH + c.get_name() + ".cs", 'w')
	f.write(s)
	f.close()